<template>
  <div class="content">
    <nut-navbar class="navbar" z-index="5" :clickable="false" fixed safe-area-inset-top>
      <template #content>
        <span>{{ t(String(route.meta.title)) }}</span>
      </template>
      <template #left>
        <iconpark-icon @click="router.go(-1)" class="close" icon-id="tongyicontyfanh" size="2.4rem"></iconpark-icon>
      </template>
      <template #right>
        <span @click="onSubmitCreate">{{t('paymentManagement_page_finish')}}</span>
      </template>
    </nut-navbar>

    <div class="top_upload">
      <nut-uploader
          accept=".jpg,.jpeg,.png,.gif,.webp"
          :before-xhr-upload="beforeUpload"
          @delete="(e) => {removeImg(e)}"
      >
      </nut-uploader>
      <p>{{t('上传俱乐部图片')}}</p>
    </div>
    <div class="form_box">
      <nut-form label-position="top" ref="formRef" >
        <nut-form-item :label="t('club_page_jlbmc')">
          <nut-input v-model.trim="createParams.name" :placeholder="t('请输入俱乐部名称')" clearable />
        </nut-form-item>
        <nut-form-item :label="t('club_page_jlbjj')">
          <nut-textarea :rows="3" v-model.trim="createParams.dec" :placeholder="t('请输入俱乐部简介')" clearable />
        </nut-form-item>
      </nut-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { ref } from "vue";
import {useRoute, useRouter} from "vue-router";
import {showNotify} from "@nutui/nutui";
import {storeToRefs} from "pinia";
import {User} from "@/store/user.ts";
import pinia from "@/store";

const route = useRoute();
const router = useRouter();
const { t } = useI18n();
const UserStore = User(pinia);
const { roleInfo } = storeToRefs(UserStore);
// 创建俱乐部表单提交
const createParams = ref({
  name: '',
  dec: '',
});

// 上传图片
const beforeUpload = (xhr: any, data: any) => {
  console.log(xhr)
  const file = data.sourceFile

  if (file && file.size > 1024 * 1024 * 2) { // 2MB限制
    showNotify.danger('文件大小不能超过2MB！')
    return;
  }
  const formData = new FormData();
  formData.append('avatar', file);
  formData.append('role_id', `${roleInfo.value.id}`);
  fetch(`http://18.162.112.52:8031/api/upload/avatar`, {
    method: 'POST',
    body: formData,
  }).then(response => response.json()).then(response => {
    if (response.status == 200 || response.status == 'success') {
      const urlImg = response.data.path
      showNotify.success(response.message)
      console.log('&&&&&', urlImg)
    } else {
      showNotify.danger(response.message)
    }
  })
}
const removeImg = (e: any) => {
  console.log('删除---', e)
}
// 创建俱乐部提交
const onSubmitCreate = () => {
  console.log('----------', createParams);
};

</script>

<style scoped lang="scss">
.content {
  font-size: 14px;
  margin: 0 0 10px;

  .top_upload {
    padding: 15% 0 0;
    p {
      font-size: 15px;
      color: rgba(192, 194, 219, 1);
      text-align: center;
      margin-top: 10px;
    }
    :deep(.nut-uploader) {
      margin: 0 auto;
      width: 120px;
      height: 120px;
      background: rgba(26, 12, 66, 1);
      border: 1px solid rgba(50, 44, 89, 1);
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;

      .nut-uploader__upload {
        background: url(/imgs/club/add_ren.webp) center no-repeat;
        background-size: 70px;
        border-radius: 50%;

        &::before {
          content: '';
          position: absolute;
          width: 32px;
          height: 32px;
          border-radius: 50%;
          bottom: 0;
          right: 6px;
          background: radial-gradient(100% 103.23% at 50% 0%, #1170FF 0%, #1154FF 61.51%, #6B11FF 98.5%);
        }
        svg {
          width: 24px;
          height: 24px;
          color: #fff !important;
          position: absolute;
          bottom: 4px;
          right: 10px;
        }
      }
      .nut-uploader__upload, .nut-uploader__preview, .nut-uploader__preview-img {
        width: 100%;
        height: 100%;
        margin-right: 0;
        .close {
          color: #fff;
        }
      }
      .nut-uploader__preview__progress {
        display: none;
      }
    }
  }
  .form_box {
    padding: 28px 10px;
    form {
      .add_title {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        .close_icon {
          width: 18px;
          height: 18px;
          background: url(/imgs/payment/close_icon.webp) center no-repeat;
          background-size: 100%;
        }
      }

      :deep(.nut-cell-group__wrap) {
        box-shadow: none;
        background: none;
        margin: 0;

        .nut-cell {
          background: none;
          padding: 0;
          margin-bottom: 14px;

          &::after {
            border: none;
          }

          &.nut-form-item {

            .nut-form-item__label {
              color: #fff;
            }
            .nut-input,
            .nut-textarea {
              color: #fff;
              width: 100%;
              background: #1d0e4a;
              border: 1px solid #322c59;
              border-radius: 8px;
              padding: 10px !important;
              .nut-textarea__textarea {
                color: #fff;
              }
            }

          }
        }
      }

    }
  }
}
</style>
